<template>
	<view>
		<view class="auth" style="display:none;">
			<view class="wanl-title">请输入账号</view>
			<form @submit="formSubmit">
				<view class="auth-group radius-bock bg-gray wlian-grey-light">
					<input placeholder="请输入手机号 / 用户名" placeholder-class="placeholder" name="account" type="text" maxlength="16"
					 confirm-type="next" @input="onKeyInput"></input>
				</view>
				<view class="auth-button flex flex-direction">
					<button form-type="submit" class="cu-btn sl radius-bock bg-orange" :disabled="submitDisabled">下一步</button>
					<!-- #ifdef MP-WEIXIN -->
					<view class="wanl-weixin-btn-info margin-tb-bj text-center text-sm">或</view>
					<button type="primary" class="cu-btn sl radius-bock bg-no" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">获取手机号一键登录</button>
					<!-- #endif -->
				</view>
				<!-- 同意服务条款 -->
				<checkbox-group :class="checked == 1 ? 'shake-horizontal' : ''" class="auth-clause" @change="CheckboxChange">
					<checkbox class="orange" :class="checked == 2 ? 'checked' : ''" :checked="checked == 2 ? true : false" value="2" />
					<view>
						我已阅读<text @tap="onDetails($store.state.common.appConfig.user_agreement, '用户协议')">用户协议</text>及<text @tap="onDetails($store.state.common.appConfig.privacy_protection, '隐私保护')">隐私权保护声明</text>
					</view>
				</checkbox-group>
			</form>
		</view>
		<view class="auth-foot">
			<view class="oauth">
				<view>微信自动登录</view>
				<view :class="item.name" class="cu-avatar lg round bg-white" v-for="(item, key) in providerList" @tap="tologin(item)" :key="key" ></view>
			</view>
			<view class="menu text-grey">
				<text @tap="register">注册</text>
				<text @tap="help">帮助</text>
			</view>
		</view>
	</view>
</template>
<script>
	const app = getApp();
	import graceChecker from '@/common/graceChecker';
	export default {
		data() {
			return {
				submitDisabled: true,
				providerList: [],
				loginRes: {},
				pageroute: '',
				checked: 0,
				req:{},
				refer_code:''
			};
		},
		onShow(){
			//console.log("isWechat",this.$jssdk.isWechat());
			this.refer_code = uni.getStorageSync('inviter_code');
			console.log("req.code:",this.refer_code.code);
			
		},
		async onLoad(option) {
			this.req = option;
			if(option.inviter_code){
				this.refer_code = option.inviter_code;
				//console.log("推荐人",this.refer_code);
				//
				uni.setStorageSync('inviter_code',this.refer_code);
			}				
			// #ifdef H5
			
			
			// 微信内置浏览器登录
			if(this.$jssdk.isWechat()){
				console.log("onload isWechat",this.$jssdk.isWechat());
				console.log("option.code:",option.code);
				
					
				let local = window.location.href; // 获取页面url
				let code = this.getUrlCode().code; // 截取code
				//app.showModal("微信",local ,function(e){alert("")});
				//if(code=="")code = "061pJWZv3Sfig43JwF1w3vKNIH1pJWZ0";
				console.log("code:",code);
				// 获取之前的code
				let oldCode = uni.getStorageSync('wanlshop:code');
				// 如果没有code，就去请求获取code
				if (code == null || code === '' || code == 'undefined' || code == oldCode) {
					//local = "https://kd.ldcr.top/h5/#/pages/user/";
					let uri = encodeURIComponent(local); 
					var s = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.$wanlshop.config('appid')}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=${encodeURIComponent(`/${this.route}?${this.$wanlshop.parseParams(this.options)}`)}#wechat_redirect`;
					console.log("url",s);
					
					uni.showLoading({   title:'微信自动登录中'		});
					setTimeout(()=>{
						window.location.href = s;	
					},500);
					//
					//061pJWZv3Sfig43JwF1w3vKNIH1pJWZ0
					uni.setStorageSync('wanlshop:code', 0); // 设置旧的code为0，避免死循环
					//let page = this.$wanlshop.prePage().$mp.page;					
					//window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.$wanlshop.config('appid')}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=${encodeURIComponent(`/${page.route}?${this.$wanlshop.parseParams(page.options)}`)}#wechat_redirect`
				} else {
					//console.log("可以了");
					//this.$wanlshop.msg("成功","公众号登录成功");
					//app.showModal("推荐人",this.refer_code ,function(e){});
					uni.showLoading({   title:'推荐人!'+this.refer_code});
					let pageroute = this.getUrlCode().state; // 截取state
					uni.setStorageSync('wanlshop:code', code); // 保存最新code
					uni.request({
						url: '/wanlshop/user/third',
						method: 'POST',
						data: {
							platform: 'h5_weixin',
							code: code,
							inviter_code:this.getReferCode(),
							client_id: uni.getStorageSync("wanlshop:chat_client_id") ? uni.getStorageSync("wanlshop:chat_client_id") : null
						},				
						success: res => {
							this.$store.dispatch('user/login', res.data);
							this.$store.dispatch('cart/login');
							this.$store.dispatch('chat/get');
							
							var url = "/city/new_pages/user";//https://kd.ldcr.top/h5/#/pages/user/";
							//this.$wanlshop.to('/pages/user');
							uni.showLoading({   title:'微信登录成功!'		});
							uni.reLaunch({url:decodeURIComponent(url)});
							/*
							if(option.url){
								uni.showLoading({   title:option.url		});
								uni.reLaunch({url:decodeURIComponent(url)});;//jvue.toUrl(option.url);
							}
							else{
								uni.reLaunch({url:decodeURIComponent(url)});
							}
							*/
							/*
							app.showModal("成功","公众号登录成功" +pageroute,function(e){			
								var url = "/pages/user";//https://kd.ldcr.top/h5/#/pages/user/";							
								uni.reLaunch({url:decodeURIComponent(url)});							
							});
							*/
							//app.showModal("成功","公众号登录成功");							
							//uni.reLaunch({url:url});
							//uni.reLaunch({url: decodeURIComponent(pageroute)});
						}
					});
					
					setTimeout(()=>{
						window.location.href = this.wanlshop_config.cdnurl +"/h5/#/pages/user";	
					},2000);
					
				}
			}else{
				let page = this.$wanlshop.prePage().$mp.page;
				this.pageroute = encodeURIComponent(`/${page.route}?${this.$wanlshop.parseParams(page.options)}`);
			}
			// 未来版本开发
			// this.providerList = [{
			// 	name: 'wlIcon-QQ',
			// 	platform: 'web_qq'
			// }];
			// #endif
			
			// 获取第三方登录
			// #ifndef H5
			let page = this.$wanlshop.prePage().$mp.page;
			this.pageroute = encodeURIComponent(`/${page.route}?${this.$wanlshop.parseParams(page.options)}`);
			uni.getProvider({
				service: 'oauth',
				success: (res) => {
					this.providerList = res.provider.map((value) => {
						let providerName = '';
						let platform = '';
						switch (value) {
							case 'weixin':
								providerName = 'wlIcon-WeChat';
								// #ifdef MP
								platform = 'mp_weixin';
								// #endif
								// #ifdef APP-PLUS
								platform = 'app_weixin';
								// #endif
								app.showModal("成功","第三方oauth");		
							
								break;
								
								
							case 'qq':
								providerName = 'wlIcon-QQ'
								// #ifdef MP
								platform = 'mp_qq';
								// #endif
								// #ifdef APP-PLUS
								platform = 'app_qq';
								// #endif
								break;
							case 'sinaweibo':
								providerName = 'wlIcon-WeiBo'
								platform = 'app_weibo';
								break;
							case 'xiaomi':
								providerName = 'wlIcon-Xiaomi'
								platform = 'app_xiaomi';
								break;
							case 'apple':
								providerName = 'wlIcon-Apple'
								platform = 'apple';
								break;
						}
						console.log(platform);
						return {
							id: value,
							name: providerName,
							platform: platform
						}
					});
				},
				fail: (error) => {
					console.log('获取登录通道失败', error);
				}
			});
			// #endif
			
			// 防止刷新登录态，uni.login code提前获取
			// #ifdef MP-WEIXIN
			uni.login({
				provider: 'weixin',
				success: res => {
					this.loginRes = res;
				},
				fail: err => {
					this.$wanlshop.msg(err.msg);
				}
			});
			// #endif
		},
		methods: {
			CheckboxChange(e) {
				this.checked = e.detail.value;
			},
			getReferCode(){
				var refer_code = uni.getStorageSync('inviter_code');
				return refer_code;
			},
			oAuthBind(code) {
				
				if (!code) return this.toAuth();
				//alert(code);
				//alert("oAuthBind:"+code);
				//this.authBind(code);
				this.auth(code);
				
			},

			/**
			 * 授权登陆获取token
			 * @param {Object} code
			 */
			auth(code) {
				
				return new Promise((resolve, reject) => {
					let loginType = Cache.get(LOGINTYPE);
					//alert('auth Promise:'+Cache.get("spread")+ ",code:"+code);
					wechatAuth(code, parseInt(Cache.get("spread")), loginType)
						.then(({
							data
						}) => {
							let expires_time = data.expires_time.substring(0, 19);
							expires_time = expires_time.replace(/-/g, '/');
							expires_time = new Date(expires_time).getTime();
							let newTime = Math.round(new Date() / 1000);
							store.commit("LOGIN", {
								token: data.token,
								time: expires_time - newTime
							});
							Cache.set(WX_AUTH, code);
							Cache.clear(STATE_KEY);
							loginType && Cache.clear(LOGINTYPE);
							resolve();
						})
						.catch(reject);
				});
			},
			
			authBind(code) {
				//alert('code'+code);
				//alert(store.state.app.uid);
				
				return new Promise((resolve, reject) => {
					
					//alert(store.state.app.uid);
					//let loginType = Cache.get(LOGINTYPE);
					//alert(loginType);
					let uid = store.state.app.uid;
					var spread = 0;
					var loginType = '';
					//alert('authBind Promise'+Cache.get("spread")+ ",code:"+code);
					//parseInt(Cache.get("spread"))
					wechatAuthBind(code, uid,spread, loginType)
						.then(({
							data
						}) => {
							//alert("绑定登录");
							let expires_time = data.expires_time.substring(0, 19);
							expires_time = expires_time.replace(/-/g, '/');
							expires_time = new Date(expires_time).getTime();
							let newTime = Math.round(new Date() / 1000);
							store.commit("LOGIN", {
								token: data.token,
								time: expires_time - newTime
							});

							resolve();
						})
						.catch(reject);
				});
			},
			
			
			/**
			 * 获取跳转授权后的地址
			 * @param {Object} appId
			 */
			getAuthUrl(appId) {
				const redirect_uri = encodeURIComponent(
					`${location.origin}/pages/auth/index?back_url=` +
					encodeURIComponent(
						encodeURIComponent(
							uni.getStorageSync(BACK_URL) ?
							uni.getStorageSync(BACK_URL) :
							location.pathname + location.search
						)
					)
				);
				
				uni.removeStorageSync(BACK_URL);
				const state = encodeURIComponent(
					("" + Math.random()).split(".")[1] + "authorizestate"
				);
				uni.setStorageSync(STATE_KEY, state);
				//alert("getAuthUrl" + appId);
				//alert("back:"+uni.getStorageSync(BACK_URL));
				//alert("uri:"+redirect_uri);
				
				return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=${state}#wechat_redirect`;
			},
			
			// 第三方登录
			tologin(provider) {
				uni.showLoading({
				    title: '第三方登录中'
				});
				// #ifndef H5
				uni.login({
					provider: provider.id,
					// #ifdef MP-ALIPAY
					scopes: 'auth_user', //支付宝小程序需设置授权类型
					// #endif
					success: (loginRes) => {
						console.log(loginRes,'授权参数')
						uni.request({
							url: '/wanlshop/user/third',
							method: 'POST',
							data: {
								platform: provider.platform,
								loginData: loginRes,
								client_id: uni.getStorageSync("wanlshop:chat_client_id") ? uni.getStorageSync("wanlshop:chat_client_id") : null
							},
							success: res => {
								uni.hideLoading();
								if (res.data.binding == 0) {
									this.$wanlshop.to(`/pages/user/auth/perfect?token=${res.data.token}&platform=${provider.platform}&url=${this.pageroute}`);
								}else{
									this.$store.dispatch('user/login', res.data);
									this.$store.dispatch('cart/login');
									this.$store.dispatch('chat/get');
									uni.reLaunch({url: decodeURIComponent(this.pageroute)});
								}
							}
						});
						// 隐藏键盘
						uni.hideKeyboard();
					},
					fail: err => {
						this.$wanlshop.msg(err.msg);
					}
				});
				// #endif
				// #ifdef H5
				uni.request({
					url: '/wanlshop/user/third_web',
					method: 'POST',
					data: {
						platform: provider.platform,
						client_id: uni.getStorageSync("wanlshop:chat_client_id")?uni.getStorageSync("wanlshop:chat_client_id") : null
					},
					success: res => {
						uni.hideLoading();
						this.$store.dispatch('user/login', res.data);
						this.$store.dispatch('cart/login');
						this.$store.dispatch('chat/get');
						// 返回页面
						uni.reLaunch({url: decodeURIComponent(this.pageroute)});
					}
				});
				// #endif
			},
			onKeyInput(e) {
				this.submitDisabled = false
			},
			// 号码登录，暂时支持微信小程序，后续版本逐渐开发
			async decryptPhoneNumber(e){
				if (e.detail.errMsg != "getPhoneNumber:fail user deny") {
					await uni.request({
						url: '/wanlshop/user/phone',
						method: 'POST',
						data: {
							encryptedData: e.detail.encryptedData,
							iv: e.detail.iv,
							code: this.loginRes.code,
							client_id: uni.getStorageSync("wanlshop:chat_client_id")?uni.getStorageSync("wanlshop:chat_client_id") : null,
							'inviter_code':uni.getStorageSync('inviter_code')?uni.getStorageSync('inviter_code'):''
						},
						success: res => {
							uni.removeStorageSync('inviter_code')
							this.$store.dispatch('user/login', res.data);
							this.$store.dispatch('cart/login');
							// 返回页面
							uni.reLaunch({url: decodeURIComponent(this.pageroute)});
						}
					});
				}
			},
			// 账户登录
			formSubmit(e) {
				if(this.checked == 2){
					//将下列代码加入到对应的检查位置
					//定义表单规则
					let rule = [{
						name: 'account',
						checkType: 'phoneno',
						errorMsg: '不是手机号码'
					}];
					//进行表单检查
					let formData = e.detail.value;
					let checkRes = graceChecker.check(formData, rule);
					// ..检查是否注册-没注册跳转注册
					if (checkRes) {
						uni.request({
							url: '/wanlshop/validate/check_mobile_exist',
							data: {
								mobile: formData.account
							},
							// 1.1.8升级
							complete: res => {
								if(res.res.code == 0){
									uni.showModal({
										title: '提示',
										content: '账号不存在，是否注册？',
										success: (res) => {
											if (res.confirm) {
												this.$wanlshop.to(`register?mobile=${formData.account}&url=${this.pageroute}`);
											} else if (res.cancel) {
												console.log('取消');
											}
										}
									});
								}else{
									this.$wanlshop.to(`phone?mobile=${formData.account}&url=${this.pageroute}`,'none');
								}
							}
						});
					} else {
						if (formData.account) {
							this.$wanlshop.to(`name?name=${formData.account}&url=${this.pageroute}`,'none');
						} else {
							this.$wanlshop.msg('请填写账号');
						}
					}
				}else{
					this.checked = 1;
					setTimeout(() => {
						this.checked = 0;
						this.$wanlshop.msg('请先同意用户协议和隐私保护声明');
						this.checked = ['2'];
					}, 300)
				}
			},
			getUrlCode() {
				// 截取url中的code方法
				var url = location.search;
				// this.winUrl = url;
				var theRequest = new Object();
				if (url.indexOf('?') != -1) {
					var str = url.substr(1);
					var strs = str.split('&');
					for (var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
					}
				}
				return theRequest;
			},
			register() {
				this.$wanlshop.to(`register?url=${this.pageroute}`);
			},
			help() {
				this.$wanlshop.to(`/pages/user/help?url=${this.pageroute}`);
			}
		}
	};
</script>

<style>
	@import url("auth.css");
</style>
